package com.hotec.PROJECT_NAME.modules.sys.service;

import com.github.pagehelper.PageHelper;
import com.hotec.PROJECT_NAME.component.basic.BaseService;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysResource;
import com.hotec.PROJECT_NAME.modules.sys.mapper.SysResourceMapper;
import com.hotec.PROJECT_NAME.utils.UtilsTreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.List;
import java.util.stream.Collectors;

;

/**
 * @author llb
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysResourceService extends BaseService<SysResource, SysResourceMapper> {
    @Autowired
    SysRoleAccountService sysRoleAccountService;

    /**
     * 查询所有资源，展示列表
     */
    @Override
    public List<SysResource> queryAll() {
        Example example = new Example(SysResource.class);
        example.createCriteria().andNotEqualTo("id", "0").andEqualTo("delFlag", "0").andEqualTo("state", "1");
        PageHelper.orderBy("sort_ asc");
        List<SysResource> all = this.mapper.selectByExample(example).stream().distinct().collect(Collectors.toList());
        return new UtilsTreeBuilder(SysResource.class).listToTree(all);
    }

    /**
     * 获取用户导航栏数据
     *
     * @return
     */
    public List<SysResource> navigate() {
        List<SysResource> list;

        PageHelper.orderBy("sort_ * 1 asc");

        if ("1".equalsIgnoreCase(getCurrentAccount(true).getId())) {
            list = mapper.selectByExample(
                    Example.builder(SysResource.class)
                        .andWhere(
                                Sqls.custom()
                                    .andEqualTo("delFlag", "0")
                        )
                        .build()
            );
        } else {
            list = mapper.queryNavigateByAccountId(this.getCurrentAccountId());
        }

        return new UtilsTreeBuilder(SysResource.class).listToTree(list);
    }
}
